package com.atguigu.gmall.order.receiver;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.model.enums.ProcessStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.order.service.OrderService;
import com.atguigu.gmall.payment.client.PaymentFeignClient;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;


/**
 * @Author LEO
 * @Date 2021年12月21日 22:50
 */
@Component
public class OrderReceiver {

    @Autowired
    private OrderService orderService;
    @Autowired
    private PaymentFeignClient paymentFeignClient;

    //第一种：绑定
    //第二种：使用延迟队列，制作配置类：直接监听队列
    @RabbitListener(queues = MqConst.QUEUE_ORDER_CANCEL)
    public void orderCancel(Long orderId,Message message,Channel channel) throws IOException {
        // 取消订单的业务逻辑
        if (orderId != null){
            //根据当前orderId  查询订单对象
            OrderInfo orderInfo = orderService.getById(orderId);
            //判断状态 订单状态和支付状态为未付款
            if (orderInfo!=null && "UNPAID".equals(orderInfo.getOrderStatus()) && "UNPAID".equals(orderInfo.getProcessStatus())){
                // 是否存在电商交易记录
                PaymentInfo paymentInfo = paymentFeignClient.getPaymentInfo(orderInfo.getOutTradeNo());
                if (paymentInfo != null && "UNPAID".equals(paymentInfo.getPaymentStatus())){
                    //说明电商本地有交易记录
//                    orderService.execExpiredOrder(orderId,"2");
                    // 调用查询支付宝的交易记录
                    Boolean result=paymentFeignClient.checkPayment(orderId);
                    // result = true
                    if(result){
                        // 判断是否能够关闭支付宝交易
                      Boolean flag  = paymentFeignClient.checkPayment(orderId);
                      // flag=true
                      if (flag){
                          //表示未支付  orderInfo payment
                          orderService.execExpiredOrder(orderId,"2");
                      }else {
                          // 表示支付成功 用户正常支付成功
                      }
                    }else {
                        // 支付宝没有交易记录 orderInfo payment
                        orderService.execExpiredOrder(orderId,"2");
                    }
                }else {
                    // 只存在orderInfo
                    // 修改订单状态 CLOSE
                    // execExpiredOrder 这个方法有关闭 orderInfo 还有关闭 paymentInfo
                    orderService.execExpiredOrder(orderId,"1");
                }
            }
        }
        // 手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }



    //监听订单消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_PAYMENT_PAY,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_PAYMENT_PAY),
            key = {MqConst.ROUTING_PAYMENT_PAY}
    ))
    public void paymentPay(Long orderId, Message message, Channel channel) throws IOException {
        // 判断
        if (orderId != null){
            // 更新订单状态
            // 根据订单orderId查询对象
            OrderInfo orderInfo = orderService.getById(orderId);
            if (orderInfo!=null && "UNPAID".equals(orderInfo.getOrderStatus()) && "UNPAID".equals(orderInfo.getProcessStatus())){
                // 修改订单状态 PAID
                orderService.updateOrderStatus(orderId, ProcessStatus.PAID);
                // 发消息给库存，库存要做什么事,减少库存数量
                orderService.sendOrderStatus(orderId);
            }
        }
        // 手动确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }


    // 监听库存系统发送的减库存结果  库存系统发送过来数据
    //监听订单消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_WARE_ORDER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_WARE_ORDER),
            key = {MqConst.ROUTING_WARE_ORDER}
    ))
    public void updateOrderStatus(String wareJson, Message message, Channel channel) throws IOException {
        // 判断
        if (StringUtils.isEmpty(wareJson)){
            Map map = JSON.parseObject(wareJson, Map.class);
            String orderId = (String) map.get("orderId");
            String status = (String) map.get("status");
            if ("DEDUCTED".equals(status)){
                //减库存成功，更新状态
                orderService.updateOrderStatus(Long.parseLong(orderId),ProcessStatus.WAITING_DELEVER);
            }else {
                // 异常情况 1.远程调用补货   2.人工客服
                orderService.updateOrderStatus(Long.parseLong(orderId),ProcessStatus.STOCK_EXCEPTION);
                // --- 订单--- 支付 -- 库存 分布式事务 --》 mq 最终一致性

            }
        }
        // 手动确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
